﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqModel;
using Cricket.LinqData;
using System.Web.Services;

public partial class AdminTossBetting : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    MatchSchedule matchSchedule;
    List<MatchSchedule> matchSchedules;
    AdminToss adminToss;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                bindMatch();
            }
        }
    }

    private void bindMatch()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetTodayMatcheForClientResult> lstResult = new List<GetTodayMatcheForClientResult>();
        lstResult = matchSchedule.GetTodayMatchesForClient();
        if (lstResult.Count > 0)
        {
            drpMatch.DataSource = lstResult;
            drpMatch.DataTextField = "Teams";
            drpMatch.DataValueField = "Id";
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }
    }
    protected void btnStart_Click(object sender, EventArgs e)
    {
        adminToss = new AdminToss(_dataObject);
        adminToss.MatchScheduleId = Convert.ToInt32(drpMatch.SelectedValue);
        bool matchIdExist = chkMatchId(Convert.ToInt32(drpMatch.SelectedValue));
        adminToss.IsOn = true;
        adminToss.CreatedDate = System.DateTime.Now;
        if (matchIdExist)
        {
            layerError.InnerHtml = "<strong>Error!</strong> Record for this match already exist."; ;
            layerError.Attributes.Add("style", "display:block;");
            layerSuccess.Attributes.Add("style", "display:none;");
            btnStop.Visible = true;
        }
        else
        {
            adminToss.Create();
            layerSuccess.InnerHtml = "<strong>Succes!</strong> Record added sucessfully."; ;
            layerSuccess.Attributes.Add("style", "display:block;");
            layerError.Attributes.Add("style", "display:none;");
            btnStop.Visible = true;
            btnStart.Visible = false;
        }


    }
    protected void btnStop_Click(object sender, EventArgs e)
    {
        adminToss = new AdminToss(_dataObject);
        adminToss.MatchScheduleId = Convert.ToInt32(drpMatch.SelectedValue);
        adminToss.Update();
        layerSuccess.InnerHtml = "<strong>Error!</strong> Record updated sucessfully."; ;
        layerSuccess.Attributes.Add("style", "display:block;");
        layerError.Attributes.Add("style", "display:none;");
        btnStop.Visible = false;
        btnStart.Visible = true;
    }

    private bool chkMatchId(int matchId)
    {
        bool isExist = false;
        List<AdminToss> lst = new List<AdminToss>();
        lst = adminToss.checkMatchExistOrNot(matchId);
        if (lst.Count > 0)
        {
            isExist = true;
        }
        else
        {
            isExist = false;
        }
        return isExist;
    }

}